﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.Biz;
using Fcdbas.Core.Xml;

namespace Fcdbas.WinUI.Common
{
    public class AppEnvironment
    {
        private static FndSession _session = null;
        public static bool IsCheck = false;
        public static FndSession Session {
            get
            {
                if (_session == null)
                {
                    _session = new FndSession();
                    _session.Machine = Fcdbas.Core.Common.HardInfo.Current.MachineName;
                    _session.OS_Version = Fcdbas.Core.Common.HardInfo.Current.OSVersion;
                    _session.IP = Fcdbas.Core.Common.HardInfo.Current.IPs;
                    _session.Program = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
                    _session.StartupPath = Application.StartupPath;
                    _session.OS_User = Environment.UserDomainName + "//" + Environment.UserName;
                    _session.Fnd_Version = "V0.1";
                }
                return _session;
            }
        }
        private static Fcdbas.Biz.FndOption _AppOption;
        public static Fcdbas.Biz.FndOption AppOption
        {
            get
            {
                if (_AppOption == null)
                {
                    _AppOption = new FndOption();
                    _AppOption.DefaultHome = "";
                    _AppOption.DateFormat = "";
                    _AppOption.DAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Near, "右边");
                    _AppOption.DateTimeFormat = "";
                    _AppOption.DeleteColor = System.Drawing.Color.Black;
                    _AppOption.FontName = "Arial";
                    _AppOption.FontSize = 10F;
                    _AppOption.EditColor = System.Drawing.Color.Black;
                    _AppOption.FocusColor = System.Drawing.Color.White;
                    _AppOption.NAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Far, "左边");
                    _AppOption.SAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Near, "右边");
                    _AppOption.TimeFormat = "";
                }
                return _AppOption;
            }
        }
        private static fnd_user_profile optionPfile;
        public static void LoadOptions()
        {
            IsCheck = Fcdbas.Core.Configuration.Config.GetConfig<bool>("IsCheck", false);
            optionPfile  = FndDAL.FindUserProfile(AppEnvironment.Session.USER_ID, "Global", "U");
            if (optionPfile == null)
            {
                optionPfile = new fnd_user_profile();

                optionPfile.identity = AppEnvironment.Session.USER_ID;
                optionPfile.pres_object = "Global";
                optionPfile.type_flag = "U";
                optionPfile.description = "配置选项";
                optionPfile.Status = Core.RecordStatus.New;
                _AppOption = new FndOption ();
                _AppOption.DefaultHome = "";
                _AppOption.DateFormat= "";
                _AppOption.DAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Far,"右边");
                _AppOption.DateTimeFormat = "";
                _AppOption.DeleteColor = System.Drawing.Color.Black;
                _AppOption.EditColor = System.Drawing.Color.Black;
                _AppOption.FocusColor = System.Drawing.Color.White;
                _AppOption.FontName = "Arial";
                _AppOption.FontSize = 10F;
                _AppOption.NAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Near, "左边");
                _AppOption.SAlign = new Controls.ComboBoxData(DevExpress.Utils.HorzAlignment.Far, "右边");
                _AppOption.TimeFormat = "";
                _AppOption.AlarmEnable = false;
                _AppOption.AlarmRiseTime = 10;
                optionPfile.info = _AppOption.ToXml();
            }
            else
            {
                
                _AppOption = optionPfile.info.ToXmlEntity<FndOption>();
            }
        }
        public static void SaveOption()
        {
            optionPfile.info = _AppOption.ToXml();
            if (optionPfile.Status == Core.RecordStatus.New)
            {
                FndDAL.UserProfileNew(optionPfile);
            }
            else
            {
                FndDAL.UserProfileModify(optionPfile);
            }
        }
    }
}
